/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;
/**
 *
 * @author Chong & Ali
 */
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;

public class Box extends GameObject{
    
    public static final int PIXEL = Level.PIXEL;
    private Image boxNormal;
    private Image boxCheck;

     /**
     * Het spelobject 'Box' wordt gecreerd. De image van de 'Box' wordt toegevoegd.
     * @param location De locatie van het spelobject moet meegegeven worden.
     */    
    public Box(Location location) {
        super(location);
        URL loc = this.getClass().getResource("box.png");
        boxNormal = new ImageIcon(loc).getImage();
        
        URL loc2 = this.getClass().getResource("boxCheck.png");
        boxCheck = new ImageIcon(loc2).getImage();
        
        super.setImage(boxNormal);
    }
    
    
   public boolean move(String direction, Location[][] locations){
   
        int x = (this.getLocation().getNeighbour(direction).getXpos())/PIXEL;
        int y = (this.getLocation().getNeighbour(direction).getYpos())/PIXEL;
        
        Location currentLoc = this.getLocation();
        Location neighboarLoc = locations[y][x];
       
        if(neighboarLoc.getGameObject() == null){
            if(neighboarLoc.getFloor() instanceof Goal){
                Goal goalA = (Goal)neighboarLoc.getFloor();
                if (goalA.isFree()){
                    if(currentLoc.getFloor() instanceof Goal){
                        Goal goalB = (Goal)currentLoc.getFloor();
                        goalB.setEmpty(true);
                        System.out.println("setFree");
                        this.setImage(boxNormal);
                        
                        Level.goalComplete--;
                    }
                    currentLoc.setGameObject(null);
                    neighboarLoc.setGameObject(this);
                    this.setLocation(neighboarLoc);
                    
                    goalA.setEmpty(false);
                    System.out.println("SetFull");
                    this.setImage(boxCheck);
                    
                    Level.goalComplete++;
                    Level.isCompleted();
                }
                return true; 
            } 
            if(currentLoc.getFloor() instanceof Goal){
                Goal goalB = (Goal)currentLoc.getFloor();
                goalB.setEmpty(true);
                System.out.println("setFree");
                this.setImage(boxNormal);
                Level.goalComplete--;
            }
            currentLoc.setGameObject(null);
            neighboarLoc.setGameObject(this);
            this.setLocation(neighboarLoc);

            return true;         
        }
        return false;                  
    }    
    
     /**
     * Deze methode is voor de kind modus. De dozen kunnen hierdoor achterelkaar verschoven worden.
     * @param direction De richting waar de doos heen wordt verschoven.
     * #return true Als 
     */ 
    public boolean superMove(String direction, Location[][] locations){
        // Door de meegegeven richting worden de coordinaten van de buren met behulp van de getNeighbour-methode opgehaald. 
        int x = (this.getLocation().getNeighbour(direction).getXpos())/PIXEL;
        int y = (this.getLocation().getNeighbour(direction).getYpos())/PIXEL;
        
        Location currentLoc = this.getLocation();
        Location neighboarLoc = locations[y][x];

        if(neighboarLoc.getGameObject() == null){ 
            if(neighboarLoc.getFloor() instanceof Goal){
                Goal goalA = (Goal)neighboarLoc.getFloor();
                if (goalA.isFree()){
                    if(currentLoc.getFloor() instanceof Goal){
                        Goal goalB = (Goal)currentLoc.getFloor();
                        goalB.setEmpty(true);
                        System.out.println("setFree");
                        this.setImage(boxNormal);
                        Level.goalComplete--;
                    }
                    currentLoc.setGameObject(null);
                    neighboarLoc.setGameObject(this);
                    this.setLocation(neighboarLoc);
                    
                    goalA.setEmpty(false);
                    System.out.println("SetFull");
                    this.setImage(boxCheck);
                
                    Level.goalComplete++;
                    Level.isCompleted();
                }
                return true; 
            }
            if(currentLoc.getFloor() instanceof Goal){
                Goal goalB = (Goal)currentLoc.getFloor();
                goalB.setEmpty(true);
                System.out.println("setFree");
                this.setImage(boxNormal);
                Level.goalComplete--;
            }
            currentLoc.setGameObject(null);
            neighboarLoc.setGameObject(this);
            this.setLocation(neighboarLoc);

            return true;   
        }
        else if (neighboarLoc.getGameObject() instanceof Box){
             Box box = (Box)neighboarLoc.getGameObject();
             if (box.superMove(direction, locations) == true){
                if(neighboarLoc.getFloor() instanceof Goal){
                    Goal goalA = (Goal)neighboarLoc.getFloor();
                        if (goalA.isFree()){
                            if(currentLoc.getFloor() instanceof Goal){
                                Goal goalB = (Goal)currentLoc.getFloor();
                                goalB.setEmpty(true);
                                System.out.println("setFree");
                                this.setImage(boxNormal);
                                Level.goalComplete--;
                            }
                            currentLoc.setGameObject(null);
                            neighboarLoc.setGameObject(this);
                            this.setLocation(neighboarLoc);
                    
                            goalA.setEmpty(false);
                            System.out.println("SetFull");
                            this.setImage(boxCheck);
                
                            Level.goalComplete++;
                            Level.isCompleted();
                        }
                    return true; 
                }
                if(currentLoc.getFloor() instanceof Goal){
                    Goal goalB = (Goal)currentLoc.getFloor();
                    goalB.setEmpty(true);
                    System.out.println("setFree");
                    this.setImage(boxNormal);
                    Level.goalComplete--;
                }
                currentLoc.setGameObject(null);
                neighboarLoc.setGameObject(this);
                this.setLocation(neighboarLoc);
                
                return true; 
            }
       }
       return false;                 
    }
}
